package com.xy.hms.service.imp;


import com.xy.hms.mapper.RoomMapper;

import com.xy.hms.model.Room;
import com.xy.hms.service.RoomServiceI;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author xiaye
 * @create 2020-03-01-12:58
 */
@Service
public class RoomServiceImp implements RoomServiceI {



    @Autowired
    RoomMapper roomMapper;

    /**
     * 查询所有房间
     * @return
     */
    @Override
    public List<Room> selectAllRooms() {

        List<Room> rooms = roomMapper.selectAllRooms();
        return rooms;
    }

    @Override
    public Room selectRoomByid(Integer rid) {
        Room room = roomMapper.selectRoomById(rid);
        return room;
    }

    /**
     * 添加房间
     * @param room 前台传来的房间对象
     * @return
     * @throws Exception 失败消息
     */
    @Transactional
    @Override
    public String insertRoom(Room room) throws Exception {
        int i = roomMapper.insertRoom(room);

        if (i==0){
            //没有添加成功
            throw new RuntimeException("添加失败，请重新填写");
        }else {
            //添加成功，发送成功的消息
            return ADD_SUCCESS;
        }

    }

    @Transactional
    @Override
    public String updateRoom(Room room) {
        int i = roomMapper.updateRoom(room);
        if (i==0){
            //更新失败
            throw new RuntimeException("更新失败，请重新操作");
        }else {
            return UPDATE_SUCCESS+i+"条信息！";
        }

    }

    @Transactional
    @Override
    public String deleteRoom(Integer rid) throws Exception {
        int i = roomMapper.deleteRoom(rid);

        if (i==0){
            //删除失败
            throw new RuntimeException("删除失败");
        }else {
            return DELETE_SUCCESS+i+"条消息";
        }

    }
}
